﻿
using DualPlaneFR4Paster.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace DualPlaneFR4Paster.Common.Services
{
    public class ParamService : IParamService
    {
        public bool AddPart(string partName)
        {
            bool result = false;
            try
            {
                using (var sysDb = new SysContext())
                {
                    //创建新文件夹
                    System.IO.Directory.CreateDirectory(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, partName));

                    var _paramPartNum = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "PartNum");
                    if (_paramPartNum != null)
                    {
                        var curPartNum = _paramPartNum.Value;
                        //复制数据                                        
                        FileHelper.DirectoryCopy(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, curPartNum), System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, partName), true);
                        sysDb.MPartNums.Add(new MPartNum
                        {
                            PartNum = partName
                        });
                        sysDb.SaveChanges();
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return result;
        }

        public bool ChangePart(string partName)
        {
            bool result = false;
            using (var sysDb = new SysContext())
            {
                var _part = sysDb.MPartNums.FirstOrDefault(p => p.PartNum == partName);
                if (_part != null)
                {
                    var _paramPartNum = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "PartNum");
                    if (_paramPartNum != null)
                    {
                        _paramPartNum.Value = partName;
                        sysDb.SaveChanges();
                        result = true;
                    }
                }
            }
            return result;
        }

        public bool DeletPart(string partName)
        {
            bool result = false;
            using (var sysDb = new SysContext())
            {
                var _part = sysDb.MPartNums.FirstOrDefault(p => p.PartNum == partName);
                if (_part != null)
                {
                    try
                    {
                        //删除文件夹
                        System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, partName));
                        di.Delete(true);
                        sysDb.MPartNums.Remove(_part);
                        sysDb.SaveChanges();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
       
                }
            }
            return result;
        }

        public List<string> GetAllParts()
        {
            List<string> allParts = new List<string>();
            using (var sysDb = new SysContext())
            {
                var _allparts = sysDb.MPartNums.ToList();
                foreach (var item in _allparts)
                {
                    allParts.Add(item.PartNum);
                }
            }
            return allParts;
        }

        public string GetPart()
        {
            string result = null;
            using (var sysDb = new SysContext())
            {
                var _paramPartNum = sysDb.MSystemParams.FirstOrDefault(p => p.Name == "PartNum");
                if (_paramPartNum != null)
                {
                    result = _paramPartNum.Value;
                }
            }
            return result;
        }
    }
}
